import os
import time
from time import sleep

import uiautomation
from pywinauto.application import Application
import win32api
import win32con
from Base.base_data import Setting as a
# 输入框输入值
import Page


def input_text(window, ipc_url):
    win32api.keybd_event(17, 0, 0, 0)  # ctrl
    win32api.keybd_event(65, 0, 0, 0)  # a
    win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
    win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
    win32api.keybd_event(8, 0, 0, 0)  # 删除
    win32api.keybd_event(8, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
    window.SendKeys(ipc_url)


# 设备接入paas
def device_into_paas_exe(device_ip):
    # 启动应用
    Application(backend="uia").start("D:\HobotCamera\HobotCamera.exe")  # .exe的路径是正确的，修改自己的路径
    time.sleep(5)

    # 建立连接
    app = Application(backend="uia").connect(title_re="地平线智能.*")
    window = uiautomation.WindowControl(searchDepth=1, ClassName='Qt5QWindowOwnDCIcon',
                                        RegexName=u'地平线智能摄像机.*')
    # 获取窗口
    win = app.window(class_name='Qt5QWindowOwnDCIcon')
    # 窗口最大化
    win.maximize()
    time.sleep(5)

    # 点击设备按钮
    device = win.child_window(title="设备", control_type="Button")
    device.draw_outline(colour="red")
    device.click_input()
    time.sleep(5)
    # 点击IP输入框
    win['Edit0'].click_input()
    time.sleep(2)
    # 输入设备ip
    window.EditControl(LocalizedControlType="编辑").SendKeys(device_ip)
    # 点击连接
    win['连接Static'].click_input()
    time.sleep(1)

    # 点击输入框
    win['账号验证Edit2'].click_input()
    # 输入账号
    window.SendKeys("admin")
    # 点击输入框
    win['账号验证Edit3'].click_input()
    # 输入密码
    window.SendKeys("admin123")

    # 点击确定
    Determine = win.child_window(title="确定", control_type="Text")
    Determine.draw_outline(colour='red')
    Determine.click_input()
    time.sleep(1)

    # 点击配置
    win['配置Static'].click_input()
    time.sleep(5)
    # 点击设备管理tab
    win['设备管理Hyperlink'].click_input()
    # 点击服务器地址输入框
    win['服务器地址:Edit'].click_input()
    # 输入地址
    input_text(window, a.ipc_test)
    # 点击端口输入框
    win['端口:Edit'].click_input()
    # 输入端口
    input_text(window, "80")
    # 点击保存
    win['保存Static'].click_input()

    # 点击身份验证tab
    win['身份验证Hyperlink'].click_input()

    # 打印原生展示端软件所有信息
    # win.print_control_identifiers()
    # time.sleep(5)

    # 点击app_id
    win['一级ID:Edit'].click_input()
    # 输入app_id
    input_text(window, a.app_id)
    # 点击app_name
    win['一级ID名称:Edit'].click_input()
    # 输入app_name
    input_text(window, "app_id")
    # 点击space_id
    win['二级ID:Edit'].click_input()
    # 输入space_id
    input_text(window, a.space_id)
    # 点击space_name
    win['二级ID名称:Edit'].click_input()
    # 输入space_name
    input_text(window, 'space_id')
    # 点击sn
    win['设备ID:Edit'].click_input()
    # 输入sn
    input_text(window, "into_sn")
    # 点击name
    win['设备名称:Edit'].click_input()
    # 输入name
    input_text(window, "into_name")
    # 点击保存
    win['保存Static'].click_input()

    # 杀死进程
    time.sleep(1)
    os.system('TASKKILL /F /IM HobotCamera.exe')
    # app.kill(True)


if __name__ == '__main__':
    device_into_paas_exe("10.64.31.50")
